import actionTypes from './constants';

//定义一个初始状态
const initState = {
    isLoading: false,
    list: [
        {
            id:1,
            title:'Lorem ininiub bibwi1',
            desc: "1111Ant Design, a design language for background applications, is refined by Ant UED Team",
            hasRead: true
        },
        {
            id:2,
            title:'Lorem ininiub bibwi2',
            desc: "222Ant Design, a design language for background applications, is refined by Ant UED Team",
            hasRead: false
        },
        {
            id:3,
            title:'Lorem ininiub bibwi3',
            desc: "333Ant Design, a design language for background applications, is refined by Ant UED Team",
            hasRead: true
        },
        {
            id:4,
            title:'Lorem ininiub bibwi4',
            desc: "4444Ant Design, a design language for background applications, is refined by Ant UED Team",
            hasRead: false
        }

    ],
}



export default (state=initState, action) =>{
    switch(action.type){
        case actionTypes.GET_INITIAL_INFORMATIONS:
            const initalData = action.payLoad.list;
            return {
                ...state,
                list: initalData
            }
        case actionTypes.START_MARK_AS_READ:
            return {
                ...state,
                isLoading: true
            }
            
        case actionTypes.FINISH_MARK_AS_READ:
            return {
                ...state,
                isLoading: false
            }
        case actionTypes.MARK_NOTIFICATIONS_AS_READ_BY_ID:
            const newList = state.list.map(item => {
                if(item.id === action.payLoad.id){
                    item.hasRead = true
                }
                return item;
            })
            return {
                ...state,
                list:newList
            };
        case actionTypes.MARK_ALL_NOTIFICATIONS_AS_READ:
            const newList2 = state.list.map(item => {
                if(item.hasRead === false){
                    item.hasRead = true;
                }
                return item
            })

            return {
                ...state,
                list: newList2
            }

        default:
            return state;
    }
}